<?php
/**
 * Created by PhpStorm.
 * User: 10037
 * Date: 2022/5/30
 * Time: 13:19
 */

namespace app\admin\controller;


use app\admin\model\Activity;
use app\admin\model\Busclass;
use app\admin\model\Businactivi;
use app\admin\model\Business;
use think\facade\Request;

class Businessc
{
    public function selectBusineClass(){
      $mode=new Busclass();
      $result= $mode->selectBusclassChild(1);

      foreach ($result as $val){
          $val['children']=($mode->selectBusclassChild($val['id']))->toArray();
      }

     return $result;

    }

    public function selectActivity(){
        $mode=new Activity();
        return $mode->selectActivityAll();
    }

    public function addBusActivi($data,$busId){
        $mode=new Businactivi();
        foreach ($data as $val) {
           $mode->addBusActivi($val['id'],$busId,$val['activeText']);
        }

    }

    public function addBusinessc(){
        $result= Request::post();
        $busActi=$result['activiBusAll'];

        unset($result['activiBusAll']);
        $result['businessClassId']=(int)$result['businessClassId'];
        $mode=new Business();
        $busId= $mode->addBusiness($result);
        $busId=(int)$busId;
        if(empty($busId)){
            return json([
                'status'=>400
            ]);
        }
        $this->addBusActivi($busActi,$busId);

        return json([
           'status'=>200
        ]);
    }


    public function selectAllBus(){
        $page=Request::post('page');
        $mode=new Business();
        $result=$mode->selectAllBus($page);
        $count=$this->selectAllBusCount();
        return json([
            'busList'=>$result,
            'count'=>$count
        ]);
    }

    public function selectAllBusCount(){
        $mode=new Business();
        return $mode->selectAllBusCount();
    }

    public function updateBusine(){
        $id=Request::post('id');
        $mode=new Business();
        $result=$mode->selectIdBus($id);
        $result['businessName']=Request::post('businessName');
        $result['businessAddress']=Request::post('businessAddress');
        $result['businessText']=Request::post('businessText');
        $result['businessPhone']=Request::post('businessPhone');
        $result['businessClassId']=Request::post('businessClassId');
        $result['buSecondClass']=Request::post('buSecondClass');
        $result['businessImage']=Request::post('businessImage');
        $flag=$result->save();
        if($flag){
            return json([
                'status'=>200
            ]);
        }
        return json([
            'status'=>400
        ]);

    }

    public function deleteBus(){
        $id= Request::post('id');
        $mode=new Business();
        $result= $mode->deleteBus($id);
        if($result){
            return json([
                'status'=>200
            ]);
        }

        return json([
            'status'=>400
        ]);
    }
}